package com.genius.reservation.common.Interceptor;

import com.genius.reservation.common.jwtUtil.jwtInterface;
import com.genius.reservation.tools.annotation.LoggerAnnotation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;


public class WebInterceptor extends HandlerInterceptorAdapter {
    private Logger logger = LoggerFactory.getLogger(WebInterceptor.class);
    @Autowired
    jwtInterface jwt;


    //重写preHandle方法，在请求发生前执行。
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {


        if(request.getMethod().toUpperCase().equals("OPTIONS")){
            return true;//通过所有OPTION请求
        }

//        HandlerMethod handlerMethod = (HandlerMethod) handler;
//        Method method = handlerMethod.getMethod();
//
//        //获取当前方法上的指定注解
//        LoggerAnnotation loggerAnnotation = method.getAnnotation(LoggerAnnotation.class);
//        JwtAnnotation annotation = method.getAnnotation(JwtAnnotation.class);
//        if (annotation != null) {
//            // 获取请求头中的Token   参数要跟前端的设置一样
//            String token = request.getHeader("X-Token");
//            if(token == null){
//                return false;
//            }
////            if(!JWTUtil.verify(token,"123456".getBytes())){
////                return false;
////            }
//            UserInformation userInformation = jwt.validateAndParse(token);
//            if (userInformation == null){
//                return false;
//            }
//            // 获取token中的用户的id
//
//            //TODO:获取到用户id 然后ID传到userdao 然后获得user对象
////            //log.info("查看token中的用户id：" + userId);
////            // 获取当前操作用户信息
////            CurrentUserInfo currentUserInfo = usersService.getCurrentUserInfo(userId);
////            // 为CurrentUserMethodArgumentResolver中的currentUserInfo设置用户信息
//            request.setAttribute("currentUserInfo", userInformation);
//
//        }
//
//        //判断当前注解是否存在
//        if (loggerAnnotation != null) {
//            long startTime = System.currentTimeMillis();
//            request.setAttribute("startTime", startTime);
//            logger.info("进入" + method.getName() + "方法的时间是：" + startTime);
//        }
        return true;
    }

    // 重写postHandle方法，在请求完成后执行。
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        if((handler instanceof HandlerMethod)){
            HandlerMethod handlerMethod = (HandlerMethod)handler;
            Method method = handlerMethod.getMethod();
            //获取当前方法上的指定注解
            LoggerAnnotation loggerAnnotation = method.getAnnotation(LoggerAnnotation.class);
            //判断当前注解是否存在
            if(loggerAnnotation != null){
                long endTime = System.currentTimeMillis();
                long startTime = (Long) request.getAttribute("startTime");
                long periodTime = endTime - startTime;
                logger.info("离开" + method.getName() + "方法的时间是：" + endTime);
                logger.info("在" + method.getName() + "方法的时长是：" + periodTime);
            }
        }

    }
}
